﻿@using LeadMaster.Properties;
@using LeadMaster.Models;
@using LeadMaster.Controllers;

@using PagedList.Mvc;
@using PagedList;
@model LeadMaster.ViewModels.ViewModelLeads

<link href="/Content/PagedList.css" rel="Stylesheet" type="text/css" />

<div style="border-style: inset; vertical-align: middle;">
<table style="border: 1px solid #C0C0C0; border-radius: 8px;">
<tr>
<th colspan="2" style="width:100px;"><img src="~/Images/create.png" class="createlead navbuttons" />
<label>Leads</label></th>
<th style="width:140px; text-align: right;">@Html.Label("Company")</th>
<td><input id="txtFilterCompany" type="text" class="filterby" data-cat="company" /></td>
<th style="width:140px; text-align: right;">@Html.Label("Surname")</th>
<td><input id="txtFilterContact" type="text" class="filterby" data-cat="contact"/></td>
<th style="width:140px; text-align: right;">@Html.Label("Postcode")</th>
<td><input id="txtFilterPostcode" type="text" class="filterby" data-cat="postcode"/></td>
<th style="width:140px; text-align: right;">@Html.Label("Int Ref")</th>
<td><input id="txtFilterAlias" type="text" class="filterby" data-cat="alias"/></td>
</tr>
</table>
</div>


<table class="listtable contactstable" cellspacing="0" cellpadding="0">
<thead>
    <tr>
    <th class="tableheader clickcolumn">&nbsp;</th>
        <th class="tableheader clickcolumn">
        <label id="SortContact" class="clickcolumn">
		Contact @if(Model.ColumnFilter=='C' && Model.ResortDirection=="u")
			{
			@:↑
			}
			else if (Model.ColumnFilter=='C' && Model.ResortDirection=="d")
			{
			@:↓
			}
		</label>
        </th>
        <th class="tableheader">
            @Html.DisplayNameFor(model => model.Contact.Telephone)
        </th>
        <th class="tableheader clickcolumn" id="SortCompany">
            @Html.DisplayNameFor(model => model.Company) @if (Model.ColumnFilter == 'M' && Model.ResortDirection == "u")
		{
		@:↑
		}
		else if (Model.ColumnFilter=='M' && Model.ResortDirection=="d")
		{
		@:↓
		}
		
        </th>
        <th class="tableheader clickcolumn" id="leadtype">
            Lead Type @if (Model.ColumnFilter == 'L' && Model.ResortDirection == "u")
		{
		@:↑
		}
		else if (Model.ColumnFilter=='L' && Model.ResortDirection=="d")
		{
		@:↓
		}
        </th>
        <th class="tableheader clickcolumn" id="SortTerritory">
            @Html.DisplayNameFor(model => model.Lead.Territory) @if (Model.ColumnFilter == 'R' && Model.ResortDirection == "u")
		{
		@:↑
		}
		else if (Model.ColumnFilter=='R' && Model.ResortDirection=="d")
		{
		@:↓
		}
        </th>
        <th class="tableheader clickcolumn" id="SortEstValue">
            @Html.DisplayName("Est Value") @if (Model.ColumnFilter == 'V' && Model.ResortDirection == "u")
		{
		@:↑
		}
		else if (Model.ColumnFilter=='V' && Model.ResortDirection=="d")
		{
		@:↓
		}
        </th>
        <th class="tableheader clickcolumn" id="SortStatus">
            @Html.DisplayName("Status") @if (Model.ColumnFilter == 'S' && Model.ResortDirection == "u")
		{
		@:↑
		}
		else if (Model.ColumnFilter=='S' && Model.ResortDirection=="d")
		{
		@:↓
		}		
        </th>
        <th class="tableheader clickcolumn" id="SortNextContactDate">
            @Html.DisplayName("Next Contact") @if (Model.ColumnFilter == 'D' && Model.ResortDirection == "u")
		{
		@:↑
		}
		else if (Model.ColumnFilter=='D' && Model.ResortDirection=="d")
		{
		@:↓
		}		
        </th>
        <th style="border-color: #AAA; width: 200px; border-top-style: solid; border-top-width: 1px;"></th>
    </tr>
</thead>
<tbody>
@foreach (Leads item in ViewBag.LeadsPage)
{
	if (item.CompanyID > 1)
	{   
    <tr style="background-color: #FFFFFF; @(LeadsController.GetColourCode(item.LeadID))">
        <td>
        @if (item.ActionRequired==true)
        {
        <img src="../../Images/Exclamation-Mark.png" data-leadid="@item.LeadID" class="actionenabled" style="width: 20px; height: 20px; cursor: pointer; vertical-align: middle;"/>
        }
        else
        {
        <img src="../../Images/Exclamation-Mark-disabled.png" data-leadid="@item.LeadID" class="actionenabled" style="width: 20px; height: 20px; cursor: pointer; vertical-align: middle;" />
        }
        
        </td>
        <td align="left">
        @if(item.Contact!=null)
        {
            <label id="contact@(item.ContactID)" data-leadid="@(item.LeadID)" data-contactid="@(item.ContactID)" class="editcontactcompanyindex" 
            style="cursor: pointer; background-color: #FFFFFF; @(LeadsController.GetColourCode(item.LeadID))">
                @item.Contact.Forename @item.Contact.Surname
            </label>
        }
        else
        {
            @:&nbsp;   
        }
        </td>
        <td align="left">
        <label class="editcontactcompanyindex" data-leadid="@(item.LeadID)" data-contactid="@(item.ContactID)" style="cursor: pointer; background-color: #FFFFFF; @(LeadsController.GetColourCode(item.LeadID))">
        @switch (item.Contact.DefaultTelephoneField)
        {
            case "Telephone":
                @item.Contact.Telephone
           break;
            case "Tel2":
                @item.Contact.Tel2
           break;
            case "Mobile1":
                @item.Contact.Mobile1
           break;
            case "Mobile2":
                @item.Contact.Mobile2
           break;
            default:
                @:&nbsp;
                break;
        }
        </label>
        </td>
        <td align="left">
            <label class="selectTab" id="edit@(item.CompanyID)" data-leadid="@(item.LeadID)" 
@if (item.Company.Company.Contains("*"))
{
@:style="cursor: pointer; background-color: #FFFF00; color: #7e30cd; font-variant: small-caps; font-weight: bold; font-size: 12px;"
}
else
{
@:style="cursor: pointer; background-color: #FFFFFF; @(LeadsController.GetColourCode(item.LeadID)) ;"
}
            data-tab="M">
               @item.IntRef @item.Company.Company
            </label>
        </td>
        <td align="left">
            <label style="cursor: pointer; background-color: #FFFFFF; @(LeadsController.GetColourCode(item.LeadID))" data-leadid="@(item.LeadID)">@item.Sector.Sector
            </label>
        </td>
        <td align="left">
            <label style="cursor: pointer; background-color: #FFFFFF; @(LeadsController.GetColourCode(item.LeadID))" data-leadid="@(item.LeadID)" data-tab="T" class="selectTab">
            @if (item.Territory != null)
            {
            @item.Territory.Substring(0, @item.Territory.Length > 30 ? 30 : @item.Territory.Length)                
                if (item.Territory.Length > 30)
                {
                    @:....
                }
            }
            </label>
        </td>
        <td align="left">
            @if (item.LeadStatusesID > 3)
            {
            <label style="cursor: pointer; background-color: #FFFFFF; @(LeadsController.GetColourCode(item.LeadID))" data-leadid="@(item.LeadID)" data-tab="F" class="selectTab">
                £@item.Fees.Sum(m => m.Quantity * m.PriceEach * item.TotalWeeks).ToString("0.##")
            </label>
            }
        </td>
        <td align="left">
            <label id="status@(item.LeadID)" style="cursor: pointer; background-color: #FFFFFF; @(LeadsController.GetColourCode(item.LeadID))" data-leadid="@(item.LeadID)"  data-tab="S" class="changestatus">
            @item.LeadStatuses.LeadStatus
            </label>
        </td>
        <td align="left">
            <label style="cursor: pointer; background-color: #FFFFFF; @(LeadsController.GetColourCode(item.LeadID))" data-leadid="@(item.LeadID)"  data-tab="D" class="selectTab">
            @if (item.Diaries.Count > 0)
            {
                @item.Diaries.OrderBy(m => m.Scheduled).FirstOrDefault().Scheduled.ToShortDateString()
            }
            </label>
        </td>
        <td width="110px" align="left">
            <img src="~/Images/comms.png" class="editlead" id="edit@(item.LeadID)" data-leadid="@(item.LeadID)" style="width: 25px; height: 25px; cursor: pointer; vertical-align: middle;" alt="Edit Lead" />
			<img src="~/Images/purchase-orders.png" style="width: 25px; height: 25px; cursor: pointer; vertical-align: middle;" alt="Purchase Orders" data-leadid="@(item.LeadID)" class="purchaseorder"/>                
            @{
                int[] pipelineArray=new int[] {4,5,6,7};
                if (this.User.IsInRole(ApplicationRoles.Operations) && item.LeadStatuses != null && Array.IndexOf(pipelineArray, item.LeadStatuses.LeadStatusesID) != -1)
                {
                <a href="http://webellas.arvixevps.com/ReportServer_SQLEXPRESS/Pages/ReportViewer.aspx?%2forac%2fAdministrator%2fdunsmore%2f@(item.ScheduleTemplate.AgreementReport)&rs:Command=Render&LeadID=@(item.LeadID)&rc:Parameters=false" target="_blank">
                <img src="../../Images/viewdoc.png" style="width: 15px; height: 15px; cursor: pointer; vertical-align: middle;" alt="View Report"/>
                </a>
                }
            }
			@if (this.User.IsInRole(ApplicationRoles.Operations))
            {
			<a href="@(Url.Action("AllocatePurchaseOrder", "Leads"))/?id=@(item.LeadID)" >
            <img src="~/Images/l.png" style="width: 15px; height: 15px; vertical-align: middle;" alt="Change Purchase Order" />
			</a>
            }
            <a href="http://webellas.arvixevps.com/ReportServer_SQLEXPRESS/Pages/ReportViewer.aspx?%2forac%2fAdministrator%2fdunsmore%2fLeadHistory&rs:Command=Render&leadid=@(item.LeadID)" target="_blank">
            <img src="../../Images/arrow_switch.png" style="width: 20px; height: 20px; cursor: pointer; vertical-align: middle;" alt="View Lead History"/>
            </a>
            <a href="http://webellas.arvixevps.com/ReportServer_SQLEXPRESS/Pages/ReportViewer.aspx?%2forac%2fAdministrator%2fdunsmore%2fClientTracker&rs:Command=Render&l=@(item.LeadID)" target="_blank">
            <img src="../../Images/Pound.ico" style="width: 20px; height: 20px; cursor: pointer; vertical-align: middle;" alt="View Lead Status"/>
            </a>
            <a href="http://webellas.arvixevps.com/ReportServer_SQLEXPRESS/Pages/ReportViewer.aspx?%2forac%2fAdministrator%2fdunsmore%2fAllClientLeads&rs:Command=Render&leadid=@(item.LeadID)" target="_blank">
            <img src="../../Images/allleads.png" style="width: 20px; height: 20px; cursor: pointer; vertical-align: middle;" alt="All Client Leads"/>
            </a>
            <a href="http://webellas.arvixevps.com/ReportServer_SQLEXPRESS/Pages/ReportViewer.aspx?%2forac%2fAdministrator%2fdunsmore%2fTrackValidReturns&rs:Command=Render&leadid=@(item.LeadID)" target="_blank">
            <img src="../../Images/leadreturns.png" style="width: 20px; height: 20px; cursor: pointer; vertical-align: middle;" alt="Valid Returns"/>
            </a>
            <a href="http://webellas.arvixevps.com/ReportServer_SQLEXPRESS/Pages/ReportViewer.aspx?%2forac%2fAdministrator%2fdunsmore%2fCommsLog&rs:Command=Render&leadid=@(item.LeadID)" target="_blank">
            <img src="../../Images/allcomms.png" style="width: 20px; height: 20px; cursor: pointer; vertical-align: middle;" alt="Communication"/>
            </a>
            <img src="../../Images/contact.png" style="width: 20px; height: 20px; cursor: pointer; vertical-align: middle;" data-tab="Q" class="selectTab" data-leadid="@(item.LeadID)" alt="Log Comm"/>
        </td>
    </tr>
  }

}
</tbody>
</table>

<div id="myPager">
@Html.PagedListPager((IPagedList)ViewBag.LeadsPage, page => Url.Action("Resort") + "/?code=" +
  Model.ColumnFilter + "&selectionStatus=" + Model.UserFilter + "&sortDirection=" + Model.ResortDirection + "&page=" + page + "&ignoreDirection=true")
</div>

<div class="smalllabel" style="float:right; visibility: hidden;">
Me <input type="radio" id="checkMe" name="userfilter" @if(Model.UserFilter.IndexOf('M')>=0 )
                                       {
                                        @:checked   
                                       }  class="userfilter"/>
Sales <input type="radio" id="checkSales" name="userfilter" @if(Model.UserFilter.IndexOf('S')>=0 )
                                       {
                                        @:checked   
                                       } class="userfilter"/>
Commercial <input type="radio" id="checkCommercial" name="userfilter" @if(Model.UserFilter.IndexOf('C')>=0 )
                                       {
                                        @:checked   
                                       } class="userfilter"/>
All <input type="radio" id="checkAll" name="userfilter" @if(Model.UserFilter.IndexOf('A')>=0 )
                                       {
                                        @:checked   
                                       } class="userfilter"/>
</div>


@Html.HiddenFor(m => m.UserFilter)
@Html.HiddenFor(m => m.ColumnFilter)
@Html.HiddenFor(m => m.ResortDirection)

<div id="leadeditdialog" class="overlay">
</div>

<div id="createleaddialog" class="overlay" title="Add New Lead">
</div>

<div id="purchaseorders"></div>

<div id="changestatus" >
@Html.DropDownList("LeadStatusesID", null, new { style = "width:180px" })
</div>


<script type="text/javascript">
    $(document).ready(function () {

        $('#changestatus').hide();

        $('#myPager a').click(function () {
            var showFilter = userfilter();
            $.ajax({
                url: this.href,
                type: 'GET',
                cache: false,
                success: function (result) {
                    $('#leadslist').html(result);
                }
            });
            return false;
        });

        $(".createlead").click(function () {
            $("#createleaddialog").dialog("open");
            $("#createleaddialog").html('<div style="text-align: center;"><div><img src="@Url.Content("~/Images/ajaxspinner_large.gif")" alt="Loading..." style="margin: 20px;" /></div><div>Loading...</div></div>');
            $('#createleaddialog').load('@(Url.Action("Index", "MarketingEnquiries"))');
        });

        $("#createleaddialog").dialog(
        {
            autoOpen: false,
            width: 750,
            height: 620,
            open: function () { $(".ui-dialog").css("box-shadow", "#000 5px 5px 5px"); },
            position: { my: 'center', at: 'center', of: window },
            modal: true,
            dialogClass: 'notitle',
            buttons: {
                save: function () {
                    $(this).dialog("close");
                    $("#formCreateLead").submit();
                },
                cancel: function () {
                    $(this).dialog("close");
                }
            }
        });

        $("#leadeditdialog").dialog(
        {
            autoOpen: false,
            width: 1050,
            height: 750,
            open: function () { $(".ui-dialog").css("box-shadow", "#000 5px 5px 5px"); },
            position: { my: 'center', at: 'center', of: window },
            modal: true,
            dialogClass: 'notitle',
            buttons: {
                save: function () {
                    $(this).dialog("close");
                    $("#formEditLead").submit();
                },
                cancel: function () {
                    $(this).dialog("close");
                }
            }
        });


        $(".editcontactcompanyindex").click(function () {
            var contactid = $(this).data("contactid");
            var leadid = $(this).data("leadid");
            $("#leadeditdialog").dialog("open");
            $("#leadeditdialog").html('<div style="text-align: center;"><div><img src="@Url.Content("~/Images/ajaxspinner_large.gif")" alt="Loading..." style="margin: 20px;" /></div><div>Loading...</div></div>');
            $('#leadeditdialog').load('@(Url.Action("Edit", "Leads"))/?id=' + leadid + '&contactID=' + contactid + '&editMode=C&code=@(Model.ColumnFilter)&selectionStatus=@(Model.UserFilter)');
        });

        $(".selectTab").click(function () {
            var editMode = $(this).data("tab");
            var leadid = $(this).data("leadid");
            $("#leadeditdialog").dialog("open");
            $("#leadeditdialog").html('<div style="text-align: center;"><div><img src="@Url.Content("~/Images/ajaxspinner_large.gif")" alt="Loading..." style="margin: 20px;" /></div><div>Loading...</div></div>');
            $('#leadeditdialog').load('@(Url.Action("Edit", "Leads"))/?id=' + leadid + '&contactID=&editMode=' + editMode + '&code=@(Model.ColumnFilter)&selectionStatus=@(Model.UserFilter)');
        });

        $(".editlead").click(function () {
            var id = $(this).data("leadid");
            $("#leadeditdialog").dialog("open");
            $("#leadeditdialog").html('<div style="text-align: center;"><div><img src="@Url.Content("~/Images/ajaxspinner_large.gif")" alt="Loading..." style="margin: 20px;" /></div><div>Loading...</div></div>');
            $('#leadeditdialog').load('@(Url.Action("Edit", "Leads"))/?id=' + id + '&contactID=&editMode=C&code=@(Model.ColumnFilter)&selectionStatus=@(Model.UserFilter)');
        });

        $("#leadtype").click(function () {
            var showFilter = userfilter();
            $("#ColumnFilter").val("L");
            $("#leadslist").html('<div style="text-align: center;"><div><img src="@Url.Content("~/Images/ajaxspinner_large.gif")" alt="Loading..." style="margin: 20px;" /></div><div>Loading...</div></div>');
            if ("@(Model.ColumnFilter)" == "L") {
                $('#leadslist').load('@(Url.Action("Resort", "Leads"))/?code=L&selectionStatus=' + showFilter + '&sortDirection=@(Model.ResortDirection)');
            }
            else {
                $('#leadslist').load('@(Url.Action("Resort", "Leads"))/?code=L&selectionStatus=' + showFilter + '&sortDirection=');
            }
        });

        $("#SortContact").click(function () {
            var showFilter = userfilter();
            $("#ColumnFilter").val("C");
            $("#leadslist").html('<div style="text-align: center;"><div><img src="@Url.Content("~/Images/ajaxspinner_large.gif")" alt="Loading..." style="margin: 20px;" /></div><div>Loading...</div></div>');
            if ("@(Model.ColumnFilter)" == "C") {
                $('#leadslist').load('@(Url.Action("Resort", "Leads"))/?code=C&selectionStatus=' + showFilter + '&sortDirection=@(Model.ResortDirection)');
            }
            else {
                $('#leadslist').load('@(Url.Action("Resort", "Leads"))/?code=C&selectionStatus=' + showFilter + '&sortDirection=');
            }
        });

        $("#SortStatus").click(function () {
            var showFilter = userfilter();
            $("#ColumnFilter").val("S");
            $("#leadslist").html('<div style="text-align: center;"><div><img src="@Url.Content("~/Images/ajaxspinner_large.gif")" alt="Loading..." style="margin: 20px;" /></div><div>Loading...</div></div>');
            if ("@(Model.ColumnFilter)" == "S") {
                $('#leadslist').load('@(Url.Action("Resort", "Leads"))/?code=S&selectionStatus=' + showFilter + '&sortDirection=@(Model.ResortDirection)');
            }
            else {
                $('#leadslist').load('@(Url.Action("Resort", "Leads"))/?code=S&selectionStatus=' + showFilter + '&sortDirection=');
            }
        });

        $("#SortCompany").click(function () {
            var showFilter = userfilter();
            $("#ColumnFilter").val("M");
            $("#leadslist").html('<div style="text-align: center;"><div><img src="@Url.Content("~/Images/ajaxspinner_large.gif")" alt="Loading..." style="margin: 20px;" /></div><div>Loading...</div></div>');
            if ("@(Model.ColumnFilter)" == "M") {
                $('#leadslist').load('@(Url.Action("Resort", "Leads"))/?code=M&selectionStatus=' + showFilter + '&sortDirection=@(Model.ResortDirection)');
            }
            else {
                $('#leadslist').load('@(Url.Action("Resort", "Leads"))/?code=M&selectionStatus=' + showFilter + '&sortDirection=');
            }
        });

        $("#SortTerritory").click(function () {
            var showFilter = userfilter();
            $("#ColumnFilter").val("R");
            $("#leadslist").html('<div style="text-align: center;"><div><img src="@Url.Content("~/Images/ajaxspinner_large.gif")" alt="Loading..." style="margin: 20px;" /></div><div>Loading...</div></div>');
            if ("@(Model.ColumnFilter)" == "R") {
                $('#leadslist').load('@(Url.Action("Resort", "Leads"))/?code=R&selectionStatus=' + showFilter + '&sortDirection=@(Model.ResortDirection)');
            }
            else {
                $('#leadslist').load('@(Url.Action("Resort", "Leads"))/?code=R&selectionStatus=' + showFilter + '&sortDirection=');
            }
        });

        $("#SortEstValue").click(function () {
            var showFilter = userfilter();
            $("#ColumnFilter").val("V");
            $("#leadslist").html('<div style="text-align: center;"><div><img src="@Url.Content("~/Images/ajaxspinner_large.gif")" alt="Loading..." style="margin: 20px;" /></div><div>Loading...</div></div>');
            if ("@(Model.ColumnFilter)" == "V") {
                $('#leadslist').load('@(Url.Action("Resort", "Leads"))/?code=V&selectionStatus=' + showFilter + '&sortDirection=@(Model.ResortDirection)');
            }
            else {
                $('#leadslist').load('@(Url.Action("Resort", "Leads"))/?code=V&selectionStatus=' + showFilter + '&sortDirection=');
            }
        });

        $("#SortNextContactDate").click(function () {
            var showFilter = userfilter();
            $("#ColumnFilter").val("D");
            $("#leadslist").html('<div style="text-align: center;"><div><img src="@Url.Content("~/Images/ajaxspinner_large.gif")" alt="Loading..." style="margin: 20px;" /></div><div>Loading...</div></div>');
            if ("@(Model.ColumnFilter)" == "D") {
                $('#leadslist').load('@(Url.Action("Resort", "Leads"))/?code=D&selectionStatus=' + showFilter + '&sortDirection=@(Model.ResortDirection)');
            }
            else {
                $('#leadslist').load('@(Url.Action("Resort", "Leads"))/?code=D&selectionStatus=' + showFilter + '&sortDirection=');
            }
        });

        $("#SortUser").click(function () {
            var showFilter = userfilter();
            $("#ColumnFilter").val("U");
            $("#leadslist").html('<div style="text-align: center;"><div><img src="@Url.Content("~/Images/ajaxspinner_large.gif")" alt="Loading..." style="margin: 20px;" /></div><div>Loading...</div></div>');
            if ("@(Model.ColumnFilter)" == "U") {
                $('#leadslist').load('@(Url.Action("Resort", "Leads"))/?code=U&selectionStatus=' + showFilter + '&sortDirection=@(Model.ResortDirection)');
            }
            else {
                $('#leadslist').load('@(Url.Action("Resort", "Leads"))/?code=U&selectionStatus=' + showFilter + '&sortDirection=');
            }
        });


        $(".userfilter").click(function () {
            var showFilter = userfilter();
            $("#leadslist").html('<div style="text-align: center;"><div><img src="@Url.Content("~/Images/ajaxspinner_large.gif")" alt="Loading..." style="margin: 20px;" /></div><div>Loading...</div></div>');
            $('#leadslist').load('@(Url.Action("Resort", "Leads"))/?code=@(Model.ColumnFilter)&selectionStatus=' + showFilter + '&sortDirection=@(Model.ResortDirection)&page=1&ignoreDirection=true');
        });

        function userfilter() {
            var ss = "";
            if ($("#checkMe").is(':checked')) {
                ss = "M";
            }
            if ($("#checkSales").is(':checked')) {
                ss += "S";
            }
            if ($("#checkCommercial").is(':checked')) {
                ss += "C";
            }
            if ($("#checkAll").is(':checked')) {
                ss += "A";
            }
            return ss;
        };

        switch ('@(Model.ColumnFilter)') {
            case 'C':
                $('#SortContact').css('color', 'blue');
                break;
            case 'M':
                $('#SortCompany').css('color', 'blue');
                break;
            case 'R':
                $('#SortTerritory').css('color', 'blue');
                break;
            case 'V':
                $('#SortEstValue').css('color', 'blue');
                break;
            case 'S':
                $('#SortStatus').css('color', 'blue');
                break;
            case 'D':
                $('#SortNextContactDate').css('color', 'blue');
                break;
            case 'U':
                $('#SortUser').css('color', 'blue');
                break;
            case 'L':
                $('#leadtype').css('color', 'blue');
                break;
            default:
        }

        $(".filterby").keypress(function (event) {
            if (event.which == 13) {
                var compval = $(this).val().replace(" ", "_");
                while (compval.indexOf(" ")>=0 ) {
                    compval = compval.replace(" ", "_");
                }
                $("#leadslist").html('<div style="text-align: center;"><div><img src="@Url.Content("~/Images/ajaxspinner_large.gif")" alt="Loading..." style="margin: 20px;" /></div><div>Loading...</div></div>');
                $('#leadslist').load('@(Url.Action("Filter", "Leads"))/?filtertext=' + compval + '&cat=' + $(this).attr("data-cat"));
            }
        });

        $(".purchaseorder").click(function () {
            var leadid = $(this).data("leadid");
            $('#purchaseorders').dialog("open");
            $('#purchaseorders').html('<div style="text-align: center;"><div><img src="@Url.Content("~/Images/ajaxspinner_large.gif")" alt="Loading..." style="margin: 20px;" /></div><div>Loading...</div></div>');
            $('#purchaseorders').load('@(Url.Action("PurchaseOrder", "Leads"))/?id=' + leadid);
        });

        $("#purchaseorders").dialog(
        {
            autoOpen: false,
            width: 1000,
            open: function () { $(".ui-dialog").css("box-shadow", "#000 5px 5px 5px"); },
            position: ['middle', 20],
            dialogClass: 'notitle',
            buttons: {
                cancel: function () {
                    $(this).dialog("close");
                }
            }
        });

        $(".changestatus").click(function () {
            var id = $(this).attr('id');
            $('#changestatus').show();
            $(this).after($("#changestatus"));
            $('#LeadStatusesID').data("leadid", $(this).data("leadid"));
        });

        $("#LeadStatusesID").blur(function () {
            $.ajax({
                type: "GET",
                url: '@(Url.Action("UpdateStatus", "Leads"))/?leadID=' + $(this).data("leadid") + "&leadStatusesID=" + $(this).val()
            });
            $("#status" + $(this).data("leadid")).text($("#LeadStatusesID option:selected").text());
            $('#changestatus').hide();
        });

        $(".actionenabled").click(function () {
            var leadAction = $(this).attr("src") == "../../Images/Exclamation-Mark-disabled.png" ? "1" : "0";
            $.ajax({
                type: "GET",
                url: '@(Url.Action("UpdateAction", "Leads"))/?leadID=' + $(this).data("leadid") + "&leadaction=" + leadAction
            });
            $(this).attr("src", $(this).attr("src") == "../../Images/Exclamation-Mark-disabled.png" ? "../../Images/Exclamation-Mark.png" : "../../Images/Exclamation-Mark-disabled.png");
        });
    });
</script>